; (function () {
	new Vue({
		el: '#jwk-park-config',
		data() {
			return {
				dataList: [],
				manualFlag: false,
				isShowDialog: false,
				addParkItem: '', //新增单个park
				cityData: [],//城市数据
				countyData: [],//区县数据
				setCountyData: [],//选择区域后筛选区县
				matingData: [], //配套列表
				text:'',
				selectMatingCode: [],
				rowIndex:null,
				//滚动条配置
				scrollDisx: 0,
				ops: {
					bar: {
						background: '#000',
						onlyShowBarOnScroll: false,
						keepShow: false,
						hoverStyle: false,
						size: '10px',
						opacity: 0.3
					},
					scrollPanel: {
						scrollingX: true,
						scrollingY: false
					}
				}
			}
		},
		methods: {
			// 获取园区数据
			getData() {
				var that = this;
				hlhttp.get({
					url: 'jwk/park_info/_search',
					data:{
						keyword:'#'
					},
					success: data => {
						data.map(item => {
							item.mating = []
							item.parkFacilityList.map(single => {
								item.mating.push(single.facility)
							})
						})
						that.dataList = data
					}
				})
			},
			//获取配套数据
			getMatingData() {
				var that = this;
				hlhttp.get({
					url: 'configure/dict',
					data: { 'module_code': 'CSTM_JWK', 'config_name': 'CSTM_JWK_PARK_FACILITY_TYPE' },
					success: data => {
						let arr = []
						data.map(item =>{
							if(item.status == 'Y'){
								arr.push(item)
							}
						})
						that.matingData = arr;
					}
				})
			},
			//获取区域数据
			getAreaData() {
				var that = this;
				hlhttp.get({
					url: 'configure/dict',
					data: { 'module_code': 'CSTM_JWK', 'config_name': 'CSTM_JWK_REGION_CITY' },
					success: data => {
						data.map(item => {
							item.name = item.dictName
							item.code = item.dictCode
						})
						that.cityData = data;
						that.getCountyData()
					}
				})
			},
			// 获取县区数据
			getCountyData() {
				var that = this;
				hlhttp.get({
					url: 'configure/dict',
					data: { 'module_code': 'CSTM_JWK', 'config_name': 'CSTM_JWK_REGION_DISTRICT' },
					success: data => {
						data.map(item => {
							item.name = item.dictName
							item.code = item.dictCode
						})
						that.countyData = data;
					}
				})
			},
			//新增/编辑园区按钮
			addParkBut(data) {
				this.selectMatingCode = []
				if(data.id){
					this.text = '编辑园区'
					this.addParkItem = {
						"id":data.id,
						"name": data.name,
						"city_code": [data.cityCode],
						"city": data.city,
						"district_code": [data.districtCode],
						"district": data.district,
						"address": data.address,
						"location": data.location,
						"traffic_info": data.trafficInfo,
						"parking_price": data.parkingPrice,
					}
					data.parkFacilityList.map(item =>{
							this.selectMatingCode.push(item.facilityCode)
					})
					this.editCountyData(data.cityCode)
				}else{
					this.text = '新增园区'
					this.addParkItem = {
						"name": "",
						"city_code": [],
						"city": "",
						"district_code": [],
						"district": "",
						"address": "",
						"location": "116.397399,39.909209",
						"traffic_info": "",
						"parking_price": "",
					}
				}
				this.isShowDialog = !this.isShowDialog

				setTimeout(() => {
					this.mapGaoge()
				}, 500)
			},
			// 校验新增填写数据
			checkAddItem() {
				console.log(this.addParkItem)
				if (!this.addParkItem.name || !this.addParkItem.city_code.length || !this.addParkItem.district_code.length || !this.addParkItem.address || !this.addParkItem.location || !this.addParkItem.traffic_info || !this.selectMatingCode.length || !this.addParkItem.parking_price) {
					console.log(this.addParkItem)
					hl.toast('请完成所有项');
					return false
				}
				return true
			},
			// 确定添加
			sureAdd() {
				if (!this.checkAddItem()) return
				let arr = []
				this.selectMatingCode.map(item => {
					this.matingData.map(single => {
						if (item == single.dictCode) {
							arr.push({
								facility_code: single.dictCode,
								facility: single.dictName
							})
						}
					})
				})
				var that = this;
				if(that.text == '新增园区'){
					hlhttp.post({
						url: 'jwk/park_info',
						data: {
							"name": this.addParkItem.name,
							"city_code": this.addParkItem.city_code + '',
							"city": this.addParkItem.city,
							"district_code": this.addParkItem.district_code + '',
							"district": this.addParkItem.district,
							"address": this.addParkItem.address,
							"location": this.addParkItem.location,
							"traffic_info": this.addParkItem.traffic_info,
							"parking_price": this.addParkItem.parking_price,
							'park_facility_list': JSON.stringify(arr)
						},
						success: data => {
							that.isShowDialog = false
							hl.toast('添加成功');
							that.getData()
						}
					})
				}else{
					hlhttp.put({
						url: 'jwk/park_info',
						data: {
							"id":this.addParkItem.id,
							"name": this.addParkItem.name,
							"city_code": this.addParkItem.city_code + '',
							"city": this.addParkItem.city,
							"district_code": this.addParkItem.district_code + '',
							"district": this.addParkItem.district,
							"address": this.addParkItem.address,
							"location": this.addParkItem.location,
							"traffic_info": this.addParkItem.traffic_info,
							"parking_price": this.addParkItem.parking_price,
							'park_facility_list': JSON.stringify(arr)
						},
						success: data => {
							that.isShowDialog = false
							hl.toast('编辑成功');
							that.getData()
						}
					})
				}

			},
			// 编辑选择城市后自动筛选区县
			editCountyData(code){
				this.setCountyData = []
				this.countyData.map(single => {
					if (single.parentDictCode == code) {
						this.setCountyData.push(single)
					}
				})
			},

			//选择城市
			selectCityCode(item) {
				this.addParkItem.city = item[0].name
				this.setCountyData = []
				this.addParkItem.district_code = []
				this.countyData.map(single => {
					if (single.parentDictCode == item[0].code) {
						this.setCountyData.push(single)
					}
				})
			},
			// 选择区县
			selectCountyCode(item) {
				this.addParkItem.district = item[0].name
			},
			//选择配套
			selectMating(code) {
				let i = this.selectMatingCode.indexOf(code)
				if (i == -1) {
					this.selectMatingCode.push(code)
				} else {
					this.selectMatingCode.splice(i, 1)
				}
			},
			// 停用
			setState(item) {
				var that = this;
				hlhttp.put({
					url: 'jwk/park_info/enable_and_disable',
					data: {
						id: item.id,
						is_valid: item.isValid == 0 ? 1 : 0
					},
					success: data => {
						hl.toast('设置成功')
						that.getData()
					}
				})
			},
			// 删除
			delItem(id){
				var that = this;
				hlhttp.delete({
					url: 'jwk/park_info?id='+ id,
					data: {
						id: id,
					},
					success: data => {
						hl.toast('删除成功')
						that.getData()
					}
				})
			},
			// 编辑
			goToedit(id){
				var that = this;
				hlhttp.get({
					url: 'jwk/park_info',
					data: {
						id: id,
					},
					success: data => {
						that.addParkBut(data)
					}
				})
			},


			//map
			mapGaoge() {
				let center = this.addParkItem.location.split(',')
				let map = new AMap.Map('mapGaode', {
					resizeEnable: true,
					center: center,
					zoom: 14
				});

				var marker = new AMap.Marker({
					position: new AMap.LngLat(map.getCenter().lng, map.getCenter().lat),
					icon: '//a.amap.com/jsapi_demos/static/demo-center/icons/poi-marker-default.png',
					offset: new AMap.Pixel(-10, -20)
				});
				map.add(marker);
			},
			// update Map
			updateMap() {
				let map = new AMap.Map('mapGaode', {
					resizeEnable: true,
					zoom: 14
				});
				var marker = new AMap.Marker({
					position: new AMap.LngLat(map.getCenter().lng, map.getCenter().lat),
					icon: '//a.amap.com/jsapi_demos/static/demo-center/icons/poi-marker-default.png',
					offset: new AMap.Pixel(-10, -20)
				});
				map.add(marker);
				this.addParkItem.location = map.getCenter().lng + ',' + map.getCenter().lat
			},
			newMap() {
				hlhttp.get({
					baseUrl: 'https://restapi.amap.com/v3/',
					url: 'geocode/geo',
					headers: {},
					dataType: "jsonp", //指定服务器返回的数据类型
					data: { address: this.addParkItem.city + this.addParkItem.district + this.addParkItem.address, key: '7e9e3de55bd3153190c60cd94285d6d6' },
					success: (data) => {
						if (data.geocodes.length) {
							var lnglat = data.geocodes[0].location;
							this.manualLocation = lnglat;
							this.usePosition()
						}
						if (!data.geocodes.length) {
							this.updateMap()
						}
					}
				})
			},
			//manual gaode
			manualGaode() {
				let center = [116.43, 39.92]
				if (this.addParkItem.location) {
					center = JSON.parse(JSON.stringify(this.addParkItem.location)).split(',')
				}
				var map = new AMap.Map("newGaode", {
					resizeEnable: true,
					center: center,
					zoom: 14
				});
				var marker = new AMap.Marker({
					position: new AMap.LngLat(map.getCenter().lng, map.getCenter().lat),
					icon: '//a.amap.com/jsapi_demos/static/demo-center/icons/poi-marker-default.png',
					offset: new AMap.Pixel(-10, -20)
				});
				map.add(marker);
				this.manualLocation = map.getCenter().lng + ',' + map.getCenter().lat;
				map.on('click', (e) => {
					this.manualLocation = e.lnglat.getLng() + ',' + e.lnglat.getLat();
					map.clearMap();
					var marker = new AMap.Marker({
						position: new AMap.LngLat(e.lnglat.getLng(), e.lnglat.getLat()),
						icon: '//a.amap.com/jsapi_demos/static/demo-center/icons/poi-marker-default.png',
						offset: new AMap.Pixel(-10, -20)
					});
					map.add(marker);
				});
			},
			//show newMap
			showNewMap() {
				this.manualFlag = true;
				setTimeout(() => {
					this.manualGaode()
				}, 30)
			},
			// hide newMap
			hideNewMap() {
				this.manualFlag = false;
			},
			//使用当前定位
			usePosition() {
				this.addParkItem.location = JSON.parse(JSON.stringify(this.manualLocation))
				this.hideNewMap()
				this.mapGaoge()
			},



			handleComplete(vertical, horizontal) {
				this.scrollDisx = horizontal.process * 100
			},
			//滚动初始化
			rollInit() {
				const self = this;
				let disx = 0;
				function scrollFunc(e, ref) {
					e = e || window.event;
					if (e && e.preventDefault) {
						if (e.deltaY > 0) {
							disx += 10
						}
						if (e.deltaY < 0) {
							disx -= 10
						}
						if (disx > 100) { disx = 100 }
						if (disx < 0) { disx = 0 }
						self.$refs[ref].scrollTo(
							{
								x: disx + '%'
							},
							10
						);
						e.preventDefault();
						e.stopPropagation();
					} else {
						e.returnvalue = false;
						return false;
					}
				}

				document.querySelector('.hl-scroll-container').onmouseenter = function () {
					disx = self.scrollDisx;
					scrollLeft('.hl-scroll-container', 'scrollWrap')
				}

				document.querySelector('.hl-scroll-container').onmouseleave = function () {
					document.querySelector('.hl-scroll-container').onmousewheel = null;
				};

				function scrollLeft(className, ref) {
					function addEventListFn(e) { scrollFunc(e, ref) }
					document.querySelector(className).onmousewheel = addEventListFn
				}
			}
		},
		mounted() {
			let style = document.createElement('style')
			style.innerHTML = `
				.__vuescroll .__panel{overflow:hidden !important}
			`
			this.$refs.vs.appendChild(style)
			this.rollInit()
			this.getAreaData()
			this.getMatingData()
			this.getData()
		}
	})
}())
